home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / gdb-4.12 / gdb / inftarg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-03  |  8.1 KB  |  325 lines

  1. /* Target-vector operations for controlling Unix child processes, for GDB.
  2.    Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
  3.    Contributed by Cygnus Support.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "defs.h"
  22. #include "frame.h"  /* required by inferior.h */
  23. #include "inferior.h"
  24. #include "target.h"
  25. #include "wait.h"
  26. #include "gdbcore.h"
  27. #include "command.h"
  28. #include <signal.h>
  29.  
  30. static void
  31. child_prepare_to_store PARAMS ((void));
  32.  
  33. #ifndef CHILD_WAIT
  34. static int child_wait PARAMS ((int, struct target_waitstatus *));
  35. #endif /* CHILD_WAIT */
  36.  
  37. static void child_open PARAMS ((char *, int));
  38.  
  39. static void
  40. child_files_info PARAMS ((struct target_ops *));
  41.  
  42. static void
  43. child_detach PARAMS ((char *, int));
  44.  
  45. static void
  46. child_attach PARAMS ((char *, int));
  47.  
  48. static void
  49. ptrace_me PARAMS ((void));
  50.  
  51. static void
  52. ptrace_him PARAMS ((int));
  53.  
  54. static void
  55. child_create_inferior PARAMS ((char *, char *, char **));
  56.  
  57. static void
  58. child_mourn_inferior PARAMS ((void));
  59.  
  60. static int
  61. child_can_run PARAMS ((void));
  62.  
  63. extern char **environ;
  64.  
  65. /* Forward declaration */
  66. extern struct target_ops child_ops;
  67.  
  68. #ifndef CHILD_WAIT
  69.  
  70. /* Wait for child to do something.  Return pid of child, or -1 in case
  71.    of error; store status through argument pointer OURSTATUS.  */
  72.  
  73. static int
  74. child_wait (pid, ourstatus)
  75.      int pid;
  76.      struct target_waitstatus *ourstatus;
  77. {
  78.   int save_errno;
  79.   int status;
  80.  
  81.   do {
  82.     if (attach_flag)
  83.       set_sigint_trap();    /* Causes SIGINT to be passed on to the
  84.                    attached process. */
  85.     pid = wait (&status);
  86.     save_errno = errno;
  87.  
  88.     if (attach_flag)
  89.       clear_sigint_trap();
  90.  
  91.     if (pid == -1)
  92.       {
  93.     if (save_errno == EINTR)
  94.       continue;
  95.     fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing: %s.\n",
  96.          safe_strerror (save_errno));
  97.     /* Claim it exited with unknown signal.  */
  98.     ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
  99.     ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
  100.         return -1;
  101.       }
  102.   } while (pid != inferior_pid); /* Some other child died or stopped */
  103.   store_waitstatus (ourstatus, status);
  104.   return pid;
  105. }
  106. #endif /* CHILD_WAIT */
  107.  
  108. /* Attach to process PID, then initialize for debugging it.  */
  109.  
  110. static void
  111. child_attach (args, from_tty)
  112.      char *args;
  113.      int from_tty;
  114. {
  115.   if (!args)
  116.     error_no_arg ("process-id to attach");
  117.  
  118. #ifndef ATTACH_DETACH
  119.   error ("Can't attach to a process on this machine.");
  120. #else
  121.   {
  122.     char *exec_file;
  123.     int pid;
  124.  
  125.     pid = atoi (args);
  126.  
  127.     if (pid == getpid())        /* Trying to masturbate? */
  128.       error ("I refuse to debug myself!");
  129.  
  130.     if (from_tty)
  131.       {
  132.     exec_file = (char *) get_exec_file (0);
  133.  
  134.     if (exec_file)
  135.       printf_unfiltered ("Attaching to program `%s', %s\n", exec_file,
  136.           target_pid_to_str (pid));
  137.     else
  138.       printf_unfiltered ("Attaching to %s\n", target_pid_to_str (pid));
  139.  
  140.     gdb_flush (gdb_stdout);
  141.       }
  142.  
  143.     attach (pid);
  144.     inferior_pid = pid;
  145.     push_target (&child_ops);
  146.   }
  147. #endif  /* ATTACH_DETACH */
  148. }
  149.  
  150.  
  151. /* Take a program previously attached to and detaches it.
  152.    The program resumes execution and will no longer stop
  153.    on signals, etc.  We'd better not have left any breakpoints
  154.    in the program or it'll die when it hits one.  For this
  155.    to work, it may be necessary for the process to have been
  156.    previously attached.  It *might* work if the program was
  157.    started via the normal ptrace (PTRACE_TRACEME).  */
  158.  
  159. static void
  160. child_detach (args, from_tty)
  161.      char *args;
  162.      int from_tty;
  163. {
  164. #ifdef ATTACH_DETACH
  165.   {
  166.     int siggnal = 0;
  167.  
  168.     if (from_tty)
  169.       {
  170.     char *exec_file = get_exec_file (0);
  171.     if (exec_file == 0)
  172.       exec_file = "";
  173.     printf_unfiltered ("Detaching from program: %s %s\n", exec_file,
  174.         target_pid_to_str (inferior_pid));
  175.     gdb_flush (gdb_stdout);
  176.       }
  177.     if (args)
  178.       siggnal = atoi (args);
  179.  
  180.     detach (siggnal);
  181.     inferior_pid = 0;
  182.     unpush_target (&child_ops);
  183.   }
  184. #else
  185.   error ("This version of Unix does not support detaching a process.");
  186. #endif
  187. }
  188.  
  189. /* Get ready to modify the registers array.  On machines which store
  190.    individual registers, this doesn't need to do anything.  On machines
  191.    which store all the registers in one fell swoop, this makes sure
  192.    that registers contains all the registers from the program being
  193.    debugged.  */
  194.  
  195. static void
  196. child_prepare_to_store ()
  197. {
  198. #ifdef CHILD_PREPARE_TO_STORE
  199.   CHILD_PREPARE_TO_STORE ();
  200. #endif
  201. }
  202.  
  203. /* Print status information about what we're accessing.  */
  204.  
  205. static void
  206. child_files_info (ignore)
  207.      struct target_ops *ignore;
  208. {
  209.   printf_unfiltered ("\tUsing the running image of %s %s.\n",
  210.       attach_flag? "attached": "child", target_pid_to_str (inferior_pid));
  211. }
  212.  
  213. /* ARGSUSED */
  214. static void
  215. child_open (arg, from_tty)
  216.      char *arg;
  217.      int from_tty;
  218. {
  219.   error ("Use the \"run\" command to start a Unix child process.");
  220. }
  221.  
  222. /* Stub function which causes the inferior that runs it, to be ptrace-able
  223.    by its parent process.  */
  224.  
  225. static void
  226. ptrace_me ()
  227. {
  228.   /* "Trace me, Dr. Memory!" */
  229.   call_ptrace (0, 0, (PTRACE_ARG3_TYPE) 0, 0);
  230. }
  231.  
  232. /* Stub function which causes the GDB that runs it, to start ptrace-ing
  233.    the child process.  */
  234.  
  235. static void
  236. ptrace_him (pid)
  237.      int pid;
  238. {
  239.   push_target (&child_ops);
  240.  
  241. #ifdef START_INFERIOR_TRAPS_EXPECTED
  242.   startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
  243. #else
  244.   /* One trap to exec the shell, one to exec the program being debugged.  */
  245.   startup_inferior (2);
  246. #endif
  247. }
  248.  
  249. /* Start an inferior Unix child process and sets inferior_pid to its pid.
  250.    EXEC_FILE is the file to run.
  251.    ALLARGS is a string containing the arguments to the program.
  252.    ENV is the environment vector to pass.  Errors reported with error().  */
  253.  
  254. static void
  255. child_create_inferior (exec_file, allargs, env)
  256.      char *exec_file;
  257.      char *allargs;
  258.      char **env;
  259. {
  260.   fork_inferior (exec_file, allargs, env, ptrace_me, ptrace_him);
  261.   /* We are at the first instruction we care about.  */
  262.   /* Pedal to the metal... */
  263.   proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
  264. }
  265.  
  266. static void
  267. child_mourn_inferior ()
  268. {
  269.   unpush_target (&child_ops);
  270.   generic_mourn_inferior ();
  271. }
  272.  
  273. static int
  274. child_can_run ()
  275. {
  276.   return(1);
  277. }
  278.  
  279. struct target_ops child_ops = {
  280.   "child",            /* to_shortname */
  281.   "Unix child process",        /* to_longname */
  282.   "Unix child process (started by the \"run\" command).",    /* to_doc */
  283.   child_open,            /* to_open */
  284.   0,                /* to_close */
  285.   child_attach,            /* to_attach */
  286.   child_detach,         /* to_detach */
  287.   child_resume,            /* to_resume */
  288.   child_wait,            /* to_wait */
  289.   fetch_inferior_registers,    /* to_fetch_registers */
  290.   store_inferior_registers,    /* to_store_registers */
  291.   child_prepare_to_store,    /* to_prepare_to_store */
  292.   child_xfer_memory,        /* to_xfer_memory */
  293.   child_files_info,        /* to_files_info */
  294.   memory_insert_breakpoint,    /* to_insert_breakpoint */
  295.   memory_remove_breakpoint,    /* to_remove_breakpoint */
  296.   terminal_init_inferior,    /* to_terminal_init */
  297.   terminal_inferior,         /* to_terminal_inferior */
  298.   terminal_ours_for_output,    /* to_terminal_ours_for_output */
  299.   terminal_ours,        /* to_terminal_ours */
  300.   child_terminal_info,        /* to_terminal_info */
  301.   kill_inferior,        /* to_kill */
  302.   0,                /* to_load */
  303.   0,                /* to_lookup_symbol */
  304.   child_create_inferior,    /* to_create_inferior */
  305.   child_mourn_inferior,        /* to_mourn_inferior */
  306.   child_can_run,        /* to_can_run */
  307.   0,                 /* to_notice_signals */
  308.   process_stratum,        /* to_stratum */
  309.   0,                /* to_next */
  310.   1,                /* to_has_all_memory */
  311.   1,                /* to_has_memory */
  312.   1,                /* to_has_stack */
  313.   1,                /* to_has_registers */
  314.   1,                /* to_has_execution */
  315.   0,                /* sections */
  316.   0,                /* sections_end */
  317.   OPS_MAGIC            /* to_magic */
  318. };
  319.  
  320. void
  321. _initialize_inftarg ()
  322. {
  323.   add_target (&child_ops);
  324. }
  325.